﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ZYHand.Service.Web.Test.Models;
using RabbitMQ.Client;
using ZYHand.Entity;
using  ZYHand.Utility;
using MongoDB.Driver;
using ZYHand.IService;
using ZYHand.DTO;

namespace ZYHand.Service.Web.Test.Controllers
{
    public class HomeToEsController : Controller
    {
        private readonly ILogger<HomeToEsController> _logger;

        public HomeToEsController(ILogger<HomeToEsController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }


        public IActionResult Push()
        {
            IRabbitMQService rabbitMQService = new RabbitMQService();

            PublishData<Order> publishData = new PublishData<Order>()
            {
                Type = EnumHelper.RabbitMQType.InsertOrder.ToString(),
                Data = new Order
                {
                    orderID = DateTime.Now.ToString("ddHHmmss"),
                    customerName = "tangzhiqiang,广东深圳，来自湖南，和老婆跟着小姨子"
                }
            };

           bool isPublish= rabbitMQService.Publish("Search", "SearchEx", "Search", JsonConvert.SerializeObject(publishData));

            return new JsonResult(new { Status = isPublish?1001:1000 });
        }

        public IActionResult Update(string orderID)
        {
            IRabbitMQService rabbitMQService = new RabbitMQService();

            PublishData<Order> publishData = new PublishData<Order>()
            {
                Type = EnumHelper.RabbitMQType.UpdateOrder.ToString(),
                Data = new Order
                {
                    orderID = orderID,
                    customerName = "liudahai,广东深圳，开发了小姨子"
                }
            };

            rabbitMQService.Publish("Search", "SearchEx", "Search", JsonConvert.SerializeObject(publishData));

            return new JsonResult(new { Status = 1001 });
        }


        public IActionResult Delete(string orderID)
        {
            IRabbitMQService rabbitMQService = new RabbitMQService();

            PublishData<Order> publishData = new PublishData<Order>()
            {
                Type = EnumHelper.RabbitMQType.DeleteOrder.ToString(),
                Data = new Order
                {
                    orderID = orderID,
                }
            };

            rabbitMQService.Publish("Search", "SearchEx", "Search", JsonConvert.SerializeObject(publishData));

            return new JsonResult(new { Status = 1001 });
        }


        [HttpGet]
        public IActionResult Pull(string wd, int from = 1, int size = 10)
        {
            //查询字符串处理
            if (string.IsNullOrEmpty(wd))
                return null;
            else
            {
                string[] wdsWhile = wd.Split(' ');  //空白字符分隔
                string[] wdsDot = wd.Split(',');    //逗号字符分隔

                IElasticSearchService elasticSearchService = new ElasticSearchService();
                SearchData<Order> searchData= elasticSearchService.Match<Order>(wd,"order",from,size);

                return new JsonResult(searchData);
            }
        }
    }
}
